package com.qubyer.okhttputil.request;

import com.qubyer.okhttputil.OkHttpUtils;
import com.qubyer.okhttputil.callback.Callback;
import com.qubyer.okhttputil.helper.SSLHelper;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zhy on 15/12/15.
 * 对OkHttpRequest的封装，对外提供更多的接口：cancel(),readTimeOut()...
 */
public class RequestCall {
    private OkHttpRequest okHttpRequest;
    private Request request;
    private Call call;

    private long readTimeOut = 10000;
    private long writeTimeOut = 10000;
    private long connTimeOut = 10000;

    private OkHttpClient clone;

    public RequestCall(OkHttpRequest request) {
        this.okHttpRequest = request;
    }

    public RequestCall readTimeOut(long readTimeOut) {
        this.readTimeOut = readTimeOut;
        return this;
    }

    public RequestCall writeTimeOut(long writeTimeOut) {
        this.writeTimeOut = writeTimeOut;
        return this;
    }

    public RequestCall connTimeOut(long connTimeOut) {
        this.connTimeOut = connTimeOut;
        return this;
    }

    public Call buildCall(Callback callback) {
        request = generateRequest(callback);
        if (readTimeOut > 0 || writeTimeOut > 0 || connTimeOut > 0) {
            readTimeOut = readTimeOut > 0 ? readTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS;
            writeTimeOut = writeTimeOut > 0 ? writeTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS;
            connTimeOut = connTimeOut > 0 ? connTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS;
            clone = null;
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//                InputStream is = null;
//                try {
//                    is = Utils.getApp().getAssets().open("qubuyer.cer");
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                clone = SSLHelper.enableTls12OnPreLollipop(OkHttpUtils.getInstance().getOkHttpClient().newBuilder()
//                        .readTimeout(readTimeOut, TimeUnit.MILLISECONDS)
//                        .writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS)
//                        .connectTimeout(connTimeOut, TimeUnit.MILLISECONDS), is).build();
//            } else {
//                clone = OkHttpUtils.getInstance().getOkHttpClient().newBuilder()
//                        .readTimeout(readTimeOut, TimeUnit.MILLISECONDS)
//                        .writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS)
//                        .connectTimeout(connTimeOut, TimeUnit.MILLISECONDS)
//                        .build();
//            }
            clone = SSLHelper.enableTls12OnPreLollipop1(OkHttpUtils.getInstance().getOkHttpClient().newBuilder()
                    .readTimeout(readTimeOut, TimeUnit.MILLISECONDS)
                    .writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS)
                    .connectTimeout(connTimeOut, TimeUnit.MILLISECONDS)).build();
//            clone = OkHttpUtils.getInstance().getOkHttpClient().newBuilder()
//                        .readTimeout(readTimeOut, TimeUnit.MILLISECONDS)
//                        .writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS)
//                        .connectTimeout(connTimeOut, TimeUnit.MILLISECONDS)
//                    .build();
            call = clone.newCall(request);
        } else {
            call = OkHttpUtils.getInstance().getOkHttpClient().newCall(request);
        }
        return call;
    }

    private Request generateRequest(Callback callback) {
        return okHttpRequest.generateRequest(callback);
    }

    public void execute(Callback callback) {
        buildCall(callback);
        if (callback != null) {
            callback.onBefore(request, getOkHttpRequest().getId());
        }
        OkHttpUtils.getInstance().execute(this, callback);
    }

    public Call getCall() {
        return call;
    }

    public Request getRequest() {
        return request;
    }

    public OkHttpRequest getOkHttpRequest() {
        return okHttpRequest;
    }

    public Response execute() throws IOException {
        buildCall(null);
        return call.execute();
    }

    public void cancel() {
        if (call != null) {
            call.cancel();
        }
    }
}
